package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceimpl implements RoleService{
    @Autowired
    RoleDao roleDao;

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        this.setRP(role.getId(),permissionIds);
        this.setRM(role.getId(),menuIds);
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public List<Permission> findPermission() {
        return roleDao.findPermission();
    }

    @Override
    public List<Menu> findMenu() {
        return roleDao.findMenu();
    }

    @Override
    public Role findById(int id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findByRPId(int id) {
        return roleDao.findByRPId(id);
    }

    @Override
    public List<Integer> findByRMId(int id) {
        return roleDao.findByRMId(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.update(role);
        roleDao.delRP(role.getId());
        roleDao.delRM(role.getId());
        this.setRP(role.getId(),permissionIds);
        this.setRM(role.getId(),menuIds);

    }

    @Override
    public int delete(int id) {
        List<Integer> list = roleDao.findByuser(id);
        if (list != null && list.size()>0){
            return 0;
        }
        roleDao.delRP(id);
        roleDao.delRM(id);
        roleDao.delete(id);
        return 1;
    }

    public void setRP(Integer roleId,Integer[] permissionIds){
        if (permissionIds != null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds){
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("permission_id",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    public void setRM(Integer roleId,Integer[] menuIds){
        if (menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds){
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("menu_id",menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }
}
